#include "crecentcalls.h"
//#include <aknnotewrappers.h>
#include <e32base.h>
#include <logcli.h>   // CLogClient
#include <logview.h>  // CLogViewRecent
#include <logwrap.h>  // CLogEvent
#include <logwraplimits.h>

#include <QDebug>

/**
 * Constructor. Defines the priority for this active object.
 */
CRecentCalls::CRecentCalls() : CActive(EPriorityStandard)
{
}

/**
 * 2nd phase constructor.
 */
CRecentCalls* CRecentCalls::NewL()
{
    CRecentCalls* self = new (ELeave) CRecentCalls();
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
}

/**
 * 2nd phase constructor.
 */
void CRecentCalls::ConstructL()
{
    CActiveScheduler::Add(this);

    User::LeaveIfError(iFs.Connect());

    // Establish connection to log engine
    iLogClient = CLogClient::NewL(iFs);

    iLogViewRecent = CLogViewRecent::NewL(*iLogClient);

    iTask = ESleep; // Default task for RunL
}

/**
 * Destructor.
 */
CRecentCalls::~CRecentCalls()
{
    Cancel();

    delete iLogViewRecent;
    delete iLogClient;

    iFs.Close();
}

/**
 * From CActive.
 */
void CRecentCalls::RunL() {
    /*switch (iTask) {
        case EGetRecent:
            {
            // Retrieve the event and handle it
            HandleRecentEventL(iLogViewRecent->Event());

            // If there are more events in the log engine database...
            if (iLogViewRecent->NextL(iStatus))
                {
                if (iStatus == KErrNone)
                    {
                    // ... set active to get the next one
                    SetActive();
                    }
                }
            else
                {
                // No more events. Go to sleep.
                iTask = ESleep;
                }
            break;
            }

        case ESleep:
        default:
            {
            break;
            }
        }*/
}

/**
 * From CActive.
 */
TInt CRecentCalls::RunError(TInt anError)
{
    return anError;
}

/**
 * From CActive.
 */
void CRecentCalls::DoCancel() {
    // Cancel the appropriate task
    switch (iTask) {
        case EGetRecent: {
            iLogViewRecent->Cancel();
            }

        case ESleep:
        default:
            {
            break;
            }
        }
}

/**
 * Reads recent events from the main event database
 */
void CRecentCalls::ReadRecentEventsL()
{
    iStatus = EReadingLog;
    iLogViewRecent->Cancel();
    qDebug() << (( iLogViewRecent != NULL ) ? "No null" : "IS NULL");
    if(iLogViewRecent->SetRecentListL(KLogNullRecentList, iStatus)) {
        if (iStatus == KErrNone) {
            // If there are events in the log view, set this active object active
            // to get the events from the main event database. See RunL().
            iTask = EGetRecent;
            SetActive();
        }
    } else {
       // StartL();
       /* _LIT(KTxt, "No recent calls.");
        CAknInformationNote* note = new (ELeave)CAknInformationNote(ETrue);
        note->ExecuteLD(KTxt);*/
        qDebug() << "No recent calls";
    }
}

/**
 * Displays a recent event in an information note.
 */
void CRecentCalls::HandleRecentEventL(const CLogEvent& anEvent) {
    /*TBuf<255> buffer;
    _LIT(KTxt, "Description: %S\nNumber: %S");
    buffer.Format(KTxt, &(anEvent.Description()), &(anEvent.Number()));
    CAknInformationNote* note = new (ELeave)CAknInformationNote(ETrue);
    note->ExecuteLD(buffer);*/
    qDebug() << "Number:";
}
